package com.dubbo.mapper;

import com.dubbo.domain.Wallet;
import com.dubbo.domain.WalletAmount;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * 钱包
 * @author lihongwu
 * 2018年12月14日 上午10:51:41
 */
public interface WalletMapper extends Mapper<Wallet> {
	
	/**
	 * 根据userId获取用户钱包
	 *
	 * @author lihongwu
	 * 2019年1月8日 下午5:48:39
	 * @param userId
	 * @return
	 */
	@Select("SELECT" + 
			"	w.id," + 
			"	w.money," + 
			"	w.waitMoney," + 
			"	w.outMoney," + 
			"	w.historyMoney," + 
			"	w.create_time," + 
			"	w.update_time" + 
			" FROM" + 
			"	w_wallet w" + 
			" LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId" + 
			" WHERE uwk.userId = #{userId}")
	Wallet getWalletByUserId(@Param("userId") String userId);
	
	/**
	 * 获取代理商加盟金
	 *
	 * @author lihongwu
	 * 2019年1月9日 下午2:33:26
	 * @param userId
	 * @return
	 */
	@Select("SELECT" + 
			"	w.money" + 
			" FROM" + 
			"	w_wallet w" + 
			" LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId" + 
			" WHERE uwk.userId = #{userId}")
	Map<String,Object> getWalletMoney(@Param("userId") String userId);
	/**
	 * 提现钱包余额减法
	 *
	 * @author lihongwu
	 * 2019年1月8日 下午5:59:46
	 * @param money
	 * @param walletId
	 * @return
	 */
	@Update("UPDATE w_wallet w" + 
			" SET w.outMoney = CAST(w.outMoney - #{money} AS DECIMAL (10, 2))," + 
			"	  w.historyMoney = CAST(w.historyMoney + #{money} AS DECIMAL (10, 2))," + 
			"	  w.update_time = NOW()" + 
			" WHERE " + 
			"		w.id = #{walletId}")
	Integer walletSubMoney(@Param("money") String money, @Param("walletId") String walletId);
	
	/**
	 * 钱包支付订单钱包余额减法
	 *
	 * @author lihongwu
	 * 2019年1月9日 上午11:00:37
	 * @param money
	 * @param walletId
	 * @return
	 */
	@Update("UPDATE w_wallet w" + 
			" SET w.money = CAST(w.money - #{money} AS DECIMAL (10, 2))," + 
			"		w.update_time = NOW()" + 
			" WHERE " + 
			"		w.id = #{walletId}")
	Integer walletPaySubMoney(@Param("money") String money, @Param("walletId") String walletId);
	
	/**
	 * 获取加盟金记录
	 *
	 * @author lihongwu
	 * 2019年1月25日 下午4:54:43
	 * @param userId
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 */
	@Select("SELECT " + 
			"	wa.id, " + 
			"	wa.amount, " + 
			"	wa.note, " + 
			"	wa.create_time, " + 
			"	o.orderNum, " + 
			"	CASE wa.note " + 
			"	WHEN '加盟金支付订单' THEN '1' " + 
			"	WHEN '加盟金退款' THEN '2' " + 
			"	WHEN '充值加盟金' THEN '3' " + 
			"	END AS 'amountType' " + 
			"FROM " + 
			"	w_amount wa " + 
			"LEFT JOIN w_wallet_key wwk ON wwk.amountId = wa.id " + 
			"LEFT JOIN u_wallet_key uwk ON wwk.walletId = uwk.walletId " + 
			"LEFT JOIN w_amount_order_key waok ON wa.id = waok.amountId " + 
			"LEFT JOIN o_order o ON waok.orderId = o.id " + 
			"WHERE " + 
			"	uwk.userId = #{userId} AND (wa.note = '充值加盟金' OR wa.note = '加盟金支付订单' OR wa.note = '加盟金退款')    " + 
			"ORDER BY " + 
			"	wa.create_time DESC " + 
			"LIMIT #{pageStart},#{pageTotal}")
	List<Map<String,Object>> getWalletDetail(@Param("userId") String userId
            , @Param("pageStart") Long pageStart
            , @Param("pageTotal") Long pageTotal);
	
	/**
	 * 获取加盟金记录总数
	 *
	 * @author lihongwu
	 * 2019年1月25日 下午4:56:12
	 * @param userId
	 * @return
	 */
	@Select("SELECT " + 
			"	COUNT(wa.id) " + 
			"FROM " + 
			"	w_amount wa " + 
			"LEFT JOIN w_wallet_key wwk ON wwk.amountId = wa.id " + 
			"LEFT JOIN u_wallet_key uwk ON wwk.walletId = uwk.walletId " + 
			"WHERE uwk.userId = #{userId} AND (wa.note = '充值加盟金' OR wa.note = '加盟金支付订单' OR wa.note = '加盟金退款') " + 
			"")
	Long getWalletDetailCount(@Param("userId") String userId);
	
	/**
	 * 根据userId和payType获取用户提现记录
	 *
	 * @author lihongwu
	 * 2019年2月23日 下午4:58:29
	 * @param userId 
	 * @param payType  提现方式  1=微信 2=支付宝
	 * @return
	 */
	@Select("SELECT " + 
			"	wa.id, " + 
			"	wa.create_time " + 
			"FROM " + 
			"	w_amount wa " + 
			"LEFT JOIN w_wallet_key wwk ON wa.id = wwk.amountId " + 
			"LEFT JOIN w_wallet w ON wwk.walletId = w.id " + 
			"LEFT JOIN u_wallet_key uwk ON w.id = uwk.walletId " + 
			"WHERE uwk.userId = #{userId} " + 
			"AND wa.note = '提现' " + 
			"AND wa.payType = #{payType} " + 
			"ORDER BY wa.create_time DESC")
	List<WalletAmount> getWithdrawDetail(@Param("userId") String userId, @Param("payType") String payType);
}